package com.lzs.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzs.entity.Notice;
import com.lzs.system.mapper.NoticeMapper;
import com.lzs.system.service.NoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzs.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 通知表 服务实现类
 * </p>
 *
 * @author ymw
 * @since 2020-12-02
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 通知分页查询
     * @param page
     * @param limit
     * @param notice
     * @return
     */
    @Override
    public IPage<Notice> selectNoticePage(Integer page, Integer limit, Notice notice) {
        Page<Notice> noticePage=new Page<>(page,limit);
        return noticeMapper.selectNoticePage(noticePage, notice);
    }

    @Override
    public R insertOrUpdateNotice(Notice notice,Long userId) {
        if (notice.getNoticeId()!=null){
            notice.setUpdateTime(new Date());
            notice.setUpdateUser(userId);
            int i = noticeMapper.updateById(notice);
            if (i>0){
                return R.ok().message("修改成功");
            }
            return R.ok().message("修改失败");
            }else {
            notice.setCreateTime(new Date());
            notice.setCreateUser(userId);
            int i = noticeMapper.insert(notice);
            if (i>0){
                return R.ok().message("新增成功");
            }
            return R.ok().message("新增失败");
        }
    }

    @Override
    public R findByNoticeId(Long noticeId){
        Notice notice = noticeMapper.selectById(noticeId);
        return R.ok().data("notice",notice);
    }
}
